import { _decorator, Component, Node, Sprite, UITransform, Animation, AnimationClip, animation, SpriteFrame, input } from 'cc';
import { EntityManager } from '../../Base/EntityManager';
import { BurstStateMachine } from './BurstStateMachine';
import { EVENT_ENUM, ENTITY_STATE_ENUM } from '../../Enums';
import { IEntity } from '../../Levels';
import DataManager from '../../Runtime/DataManager';
import EventManager from '../../Runtime/EventManager';
import { TILE_HEIGHT, TILE_WIDTH } from '../Tile/TileManager';



const { ccclass, property } = _decorator;



@ccclass('BurstManager')
export class BurstManager extends EntityManager {


    async init(params: IEntity) {

        this.fsm = this.addComponent(BurstStateMachine);
        await this.fsm.init();
        super.init(params);
        const transform = this.getComponent(UITransform);
        transform.setContentSize(TILE_WIDTH, TILE_HEIGHT);
        EventManager.Instance.on(EVENT_ENUM.PLAYER_MOVE_END, this.onBurst, this)

    }

    protected onDestroy(): void {
        super.onDestroy();
        EventManager.Instance.off(EVENT_ENUM.PLAYER_MOVE_END, this.onBurst)
    }

    
    protected update(dt: number): void {
        this.node.setPosition(this.x * TILE_WIDTH, -this.y * TILE_HEIGHT)
    }


    onBurst() {
        if(this.state === ENTITY_STATE_ENUM.DEATH || !DataManager.Instance.player) return
        const { x: playerX, y: playerY} = DataManager.Instance.player;
        if(this.x === playerX && this.y === playerY && this.state === ENTITY_STATE_ENUM.IDLE) {
            this.state = ENTITY_STATE_ENUM.ATTACK
        } else if(this.state === ENTITY_STATE_ENUM.ATTACK) {
            this.state = ENTITY_STATE_ENUM.DEATH;
            if(this.x === playerX && this.y === playerY) {
                EventManager.Instance.emit(EVENT_ENUM.ATTACK_PLAYER, ENTITY_STATE_ENUM.AIRDEATH)
            }
        }

    }



}


